package com.example.lp.lpmvvm.one.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.ViewGroup;

import com.example.lp.lpmvvm.one.holder.BaseHolder;
import com.example.lp.lpmvvm.one.viewmodel.PictureModelView;
import com.example.lp.lpmvvm.one.view.PictureView;
import com.example.lp.lpmvvm.one.viewmodel.TitleModelView;
import com.example.lp.lpmvvm.one.view.TitleView;
import com.example.lp.lpmvvm.one.viewmodel.BaseViewModel;

import java.util.ArrayList;
import java.util.List;

/**
 * Author by lp,on 2021/6/15/015,17:48.
 */
public class TitleViewAdapter extends RecyclerView.Adapter<BaseHolder> {
    private static final String TAG = "TitleViewAdapter";
    private List<BaseViewModel> datas;

    public TitleViewAdapter() {
        initData();
    }

    private void initData() {
        datas = new ArrayList<>();
        int i = 10;
        for (int i1 = 0; i1 < i; i1++) {
            if (i1 % 2 == 0) {
                datas.add(new PictureModelView("图片标题：" + i1, "图片内容：...." + i1, "https://api.dujin.org/bing/1920.php"));
            } else {
                datas.add(new TitleModelView("标题：" + i1, "内容：...." + i1));
            }
        }
        notifyDataSetChanged();

    }

    @NonNull
    @Override
    public BaseHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int type) {
        BaseHolder baseHolder = null;
        switch (type) {
            case 1:
                baseHolder = new BaseHolder(new TitleView(viewGroup.getContext()));
                break;
            case 2:
                baseHolder = new BaseHolder(new PictureView(viewGroup.getContext()));
                break;
        }
        Log.i(TAG, "onCreateViewHolder: ");
        return baseHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull BaseHolder baseHolder, int i) {
        Log.i(TAG, "onBindViewHolder: " + i);
        baseHolder.bind(datas.get(i));
    }

    @Override
    public int getItemViewType(int position) {
        if (datas != null) {
            return datas.get(position).type;
        }
        return super.getItemViewType(position);
    }

    @Override
    public int getItemCount() {
        if (datas != null) {
            return datas.size();
        }
        return 0;
    }
}
